<uieip type="page" value="login" checkcross="true" method="get"></uieip>
<uieip type="reg" value="login,data,resource"></uieip>
<script runat="server">
    /*
     * 本地注册接口
    */
    response.contentType('application/json');

    var res = login.isadmin();
    if (res.error != 0) {
        response.body(JSON.stringify(res));
    } else {
        res = get_server_info();
        if (res.error != 0) {
            response.body(JSON.stringify(res));
        } else {
            response.body(res.value);
        }
    }

    function get_server_info() {
        var sid = request.param("sid");
        var where = "sid='" + sid + "'";

        res = data.select("yqbase", "licServer", where, "");
        if (res.error != 0) {
            return res;
        }
        if (res.rows.length == 0) {
            return {
                error: 100,
                info: "服务未注册. 请登录 www.yqsite.com 进行注册。"
            }
        }

        var row = res.rows[0];

        var info = "";
        if (row.ip === undefined || row.ip === null || row.ip === "") {
            info += ("" ? "、" : info) + "ip";
        }
        if (row.tag === undefined || row.tag === null || row.tag === "") {
            info += ("" ? "、" : info) + "tag";
        }
        if (row.mark === undefined || row.mark === null || row.mark === "") {
            row.mark = "";
        }
        if (row.duedate === undefined || row.duedate === null || row.duedate === "") {
            info += ("" ? "、" : info) + "dueDate";
        }
        if (info != "") {
            return {
                error: 101,
                info: "注册信息[" + info + "]不能为空。"
            }
        }

        var r_row = {
            type: (row.ip.indexOf(':') > 0 ? 2 : 1),
            discription: row.discription,
            ip: row.ip,
            mac: row.mac,
            tag: row.tag,
            domainName: row.domainname,
            checkDomain: row.checkdomain ? true : false,
            contact: row.contact,
            phone: row.phone,
            email: row.email,
            appDate: row.appdate.split(' ')[0],
            dueDate: row.duedate.split(' ')[0],
            mark: row.mark === "" ? null : row.mark,
            sign: sFingerPrint(row.mark + ">YQ>" + row.dueDate + "->" + row.tag),
        };

        var result = JSON.stringify(r_row);

        return {
            error: 0,
            info: "success",
            value: result
        }
    }

    function sFingerPrint(text) {
        try {
            const res = resource.stringToMD5(text);
            if (res.error == 0) {
                return res.value;
            } else {
                return "@error:" + res.info;
            }
        } catch (ex) {
            return Promise.resolve("@error:" + ex.message);
        }
    }
</script>